package com.feihong.yw189.paidan.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.feihong.yw189.R;
import com.feihong.yw189.bluetooth.utils.util.ToastUtils;
import com.feihong.yw189.paidan.inter.TuiDanListListener;


/**
 * 2017/2/17.
 * 提示(账号绑定)
 */

public class TuiDanDialog extends Dialog {

    private EditText tv_tdly;
    private TextView td_ok;
    private TextView td_cancle;
    private TextView td_title;
    private TextView td_ts;
    private String title = "";
    private String content = "";
    private Activity context;
    private TuiDanListListener mTuiDanListListener;
    private RadioGroup rg_tuidan;
    private int type = 0;
    private LinearLayout ll_tuidan;

    public TuiDanDialog(Context context, String title, String content, TuiDanListListener mTuiDanListListener) {
        super(context, R.style.QiaoDaoDialogStyle);
        this.context = (Activity) context;
        this.title = title;
        this.content = content;
        this.mTuiDanListListener = mTuiDanListListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tuidan_dialog);
        tv_tdly = (EditText) findViewById(R.id.tv_tdly);
        rg_tuidan = (RadioGroup) findViewById(R.id.rg_tuidan);
        ll_tuidan = (LinearLayout) findViewById(R.id.ll_tuidan);
        td_ok = (TextView) findViewById(R.id.td_ok);
        td_title = (TextView) findViewById(R.id.td_title);
        td_cancle = (TextView) findViewById(R.id.td_cancle);
        td_ts = (TextView) findViewById(R.id.td_ts);

        if ("提示".equals(title)) {
            ll_tuidan.setVisibility(View.GONE);
            tv_tdly.setVisibility(View.GONE);
            td_ts.setVisibility(View.VISIBLE);
            if (content != null && !TextUtils.isEmpty(content)) {

                if (content.contains("订单")) {
                    td_ts.setText(content);
                } else {
                    td_ts.setText("您将拨打电话:" + content + ",确认要拨出吗?");
                }
                td_ok.setText("确认");
                td_cancle.setText("取消");
            } else {
                td_ts.setText("是否确认取消，取消后无法再次操作，订单作废，请谨慎操作");
                td_ok.setText("是");
                td_cancle.setText("否");
            }
        } else {
            td_ok.setText("确认");
            td_cancle.setText("取消");
            if ("退单".equals(title)) {
                ll_tuidan.setVisibility(View.VISIBLE);
            } else {
                ll_tuidan.setVisibility(View.GONE);
            }
            tv_tdly.setVisibility(View.VISIBLE);
            td_ts.setVisibility(View.GONE);
            tv_tdly.setHint(content);
        }
        td_title.setText(title);

        rg_tuidan.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_1:
                        type = 1;
                        break;
                    case R.id.rb_2:
                        type = 2;
                        break;
                    case R.id.rb_3:
                        type = 3;
                        break;
                    case R.id.rb_4:
                        type = 4;
                        break;
                    case R.id.rb_5:
                        type = 5;
                        break;
                    case R.id.rb_6:
                        type = 6;
                        break;
                }
            }
        });

        td_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("退单".equals(title)) {
                    // 必须选一种
                    if (type == 0) {
                        closeInputMethod(td_ok);
                        ToastUtils.showShortToast(context, "必须选择一种退单理由!");
                    } else {
                        String trim = tv_tdly.getText().toString().trim();
                        if (trim != null && !TextUtils.isEmpty(trim)) {
                            mTuiDanListListener.onCancleOrder(trim, type);
                            closeInputMethod(td_ok);
                            cancel();
                        } else {
                            ToastUtils.showShortToast(context, "请输入退单理由!");
                        }
                    }
                } else {
                    // 转派,备注,取消,预定
                    closeInputMethod(td_ok);
                    cancel();
                    String trim = tv_tdly.getText().toString().trim();
                    mTuiDanListListener.onCancleOrder(trim, type);
                }

            }
        });

        td_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeInputMethod(td_cancle);
                cancel();
            }
        });
    }

    private void closeInputMethod(View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            // imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//没有显示则显示
            imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
